package com.ls.service.impl;


import com.ls.dao.ProductDao;
import com.ls.entity.JsonResult;
import com.ls.entity.Page;
import com.ls.entity.Product;
import com.ls.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("productService")
public class ProductServiceImpl implements IProductService {
	@Autowired
	ProductDao dao;

	public JsonResult<List<Product>> findProduct() {

		JsonResult<List<Product>> result = new JsonResult<List<Product>>();
		List<Product> list = dao.findProduct();
		if (list != null) {
			result.setStatus(0);
			result.setData(list);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询产品出错");
		return result;
	}

	public JsonResult<List<Product>> findProductFenye(int pageId, int num) {
		JsonResult<List<Product>> result = new JsonResult<List<Product>>();
		Page page = new Page();
		int jilu = dao.findProduct().size();
		List<Product> list = dao.findProductFenye((pageId - 1) * num, num);
		if (list != null) {
			page.setPageId(pageId);
			page.setPageNum(jilu % num == 0 ? jilu / num : jilu / num + 1);
			result.setPage(page);
			result.setStatus(0);
			result.setData(list);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询产品分页出错");
		return result;
	}

	public JsonResult<Object> deleteByIdPr(String[] product_id) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.deleteByIdPr(product_id);
		// 失败
		if (i == 0) {
			result.setStatus(1);
			result.setMsg("删除产品失败");
			return result;
		}
		result.setStatus(0);
		result.setData(i);
		return result;
	}

	public JsonResult<List<Product>> findName(String name, int pageId, int num) {
		JsonResult<List<Product>> result = new JsonResult<List<Product>>();
		Page page = new Page();
		int jilu = dao.findName(name).size();
		List<Product> list = dao.findNameFenye(name, (pageId - 1) * num, num);
		if (list != null) {
			page.setPageId(pageId);
			page.setPageNum(jilu % num == 0 ? jilu / num : jilu / num + 1);
			result.setStatus(0);
			result.setData(list);
			result.setPage(page);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询失败");
		return result;
	}

	public JsonResult<Object> insertProduct(Product product) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.insertProduct(product);
		if (i==1) {
			result.setStatus(0);
			result.setData(i);
			return result;
		}
		result.setStatus(1);
		result.setMsg("添加产品失败了");
		return result;
	}

	public JsonResult<Product> showPr(int product_id) {
		JsonResult<Product> result = new JsonResult<Product>();
		Product product = dao.showPr(product_id);
		if (product!=null) {
			result.setStatus(0);
			result.setData(product);
			return result;
		}
		result.setStatus(1);
		result.setMsg("show产品失败了");
		return result;
	}

	public JsonResult<Object> updateProduct(Product product) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.update(product);
		if (i==1) {
			result.setStatus(0);
			result.setData(i);
			return result;
		}
		result.setStatus(1);
		result.setMsg("修改产品失败了");
		return result;
	}
	


}
